﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Iridescent.Models;

namespace Iridescent
{
    public partial class TimetablePage : PhoneApplicationPage
    {
        public TimetablePage()
        {
            InitializeComponent();
            DataContext = App.TimetableViewModel;
        }

        private void AppBarNewIconButton_Click(object sender, System.EventArgs e)
        {
            var day = App.TimetableViewModel.CourseListViewModels
                [App.TimetableViewModel.SelectedCourseListIndex].Day;

            NavigationService.Navigate(
            new Uri("/Views/NewOrEditCoursePage.xaml?day=" + day.ToString(), UriKind.RelativeOrAbsolute));
        }

        private void EditMenuItem_Click(object sender, System.EventArgs e)
        {
            var menuItem = (MenuItem)sender;
            var course = (Course)menuItem.DataContext;
            NavigationService.Navigate(
            new Uri("/Views/NewOrEditCoursePage.xaml?id=" + course.Id.ToString(), UriKind.RelativeOrAbsolute));
        }

        private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            // TODO: Add event handler implementation here.
            var stackPanel = (StackPanel)sender;
            var course = (Course)stackPanel.DataContext;

            NavigationService.Navigate(
            new Uri("/Views/CourseHubPage.xaml?coursename=" + course.Name, UriKind.RelativeOrAbsolute));
        }

        private void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var menuItem = (MenuItem)sender;
            var course = (Course)menuItem.DataContext;

            App.TimetableViewModel
                .CourseListViewModels[App.TimetableViewModel.SelectedCourseListIndex]
                .Courses.Remove(course);

            App.LocalDatabase.Courses.DeleteOnSubmit(course);
            App.LocalDatabase.SubmitChanges();
        }
    }
}
